Back to index

texmacs  1.0.7.15
Public Member Functions | Public Attributes | Friends
socket_link_rep Struct Reference

#include <socket_link.hpp>

Inheritance diagram for socket_link_rep:
Inheritance graph
[legend]
Collaboration diagram for socket_link_rep:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 socket_link_rep (string host, int port, int type, int fd)
 ~socket_link_rep ()
string start ()
void write (string s, int channel)
stringwatch (int channel)
string read (int channel)
void listen (int msecs)
void interrupt ()
void stop ()
void feed (int channel)
void write_packet (string s, int channel)
bool complete_packet (int channel)
string read_packet (int channel, int timeout, bool &success)
void secure_server (string cmd)
void secure_client ()
void set_command (command _cmd)
void apply_command ()

Public Attributes

string host
int port
int type
int io
string outbuf
socket_notifier sn
bool alive
string secret
command feed_cmd
int ref_count

Friends

class tm_link

Detailed Description

Definition at line 21 of file socket_link.hpp.


Constructor & Destructor Documentation

socket_link_rep::socket_link_rep ( string  host,
int  port,
int  type,
int  fd 
)

Definition at line 39 of file socket_link.cpp.

                                                                           :
  host (host2), port (port2), type (type2)
{
  socket_link_set->insert ((pointer) this);
  io     = fd;
  outbuf = "";
  alive  = (fd != -1);
  if (type == SOCKET_SERVER) call ("server-add", object (io));
  else if (type == SOCKET_CLIENT) call ("client-add");
}

Here is the call graph for this function:

Definition at line 50 of file socket_link.cpp.

                                   {
  stop ();
  socket_link_set->remove ((pointer) this);
}

Here is the call graph for this function:


Member Function Documentation

void tm_link_rep::apply_command ( ) [inline, inherited]

Definition at line 64 of file tm_link.hpp.

{ if (!is_nil (feed_cmd)) feed_cmd->apply (); }

Here is the call graph for this function:

Here is the caller graph for this function:

bool tm_link_rep::complete_packet ( int  channel) [inherited]

Definition at line 52 of file tm_link.cpp.

                                         {
  return message_complete (watch (channel));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void socket_link_rep::feed ( int  channel)

Definition at line 176 of file socket_link.cpp.

                                  {
#ifndef __MINGW32__
  if ((!alive) || (channel != LINK_OUT)) return;
  char tempout[1024];
  int r= recv (io, tempout, 1024, 0);
  if (r <= 0) {
    if (r == 0) cout << "TeXmacs] '" << host << ":" << port << "' hung up\n";
    else cerr << "TeXmacs] read failed from '" << host << ":" << port << "'\n";
    stop ();
  }
  else if (r != 0) {
    if (DEBUG_IO) cout << debug_io_string (string (tempout, r));
    outbuf << string (tempout, r);
  }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void socket_link_rep::interrupt ( ) [virtual]

Implements tm_link_rep.

Definition at line 226 of file socket_link.cpp.

                            {
}
void socket_link_rep::listen ( int  msecs) [virtual]

Implements tm_link_rep.

Definition at line 211 of file socket_link.cpp.

                                  {
#ifndef __MINGW32__
  if (!alive) return;
  fd_set rfds;
  FD_ZERO (&rfds);
  FD_SET (io, &rfds);
  struct timeval tv;
  tv.tv_sec  = msecs / 1000;
  tv.tv_usec = 1000 * (msecs % 1000);
  int nr= select (io+1, &rfds, NULL, NULL, &tv);
  if (nr != 0 && FD_ISSET (io, &rfds)) feed (LINK_OUT);
#endif
}

Here is the call graph for this function:

string socket_link_rep::read ( int  channel) [virtual]

Implements tm_link_rep.

Definition at line 201 of file socket_link.cpp.

                                  {
  if (channel == LINK_OUT) {
    string r= outbuf;
    outbuf= "";
    return r;
  }
  else return "";
}
string tm_link_rep::read_packet ( int  channel,
int  timeout,
bool &  success 
) [inherited]

Definition at line 57 of file tm_link.cpp.

                                                                 {
  success= false;
  string& r= watch (channel);
  time_t start= texmacs_time ();
  while (!message_complete (r)) {
    int n= N(r);
    if (timeout > 0) listen (timeout);
    if (N(r) == n && (texmacs_time () - start >= timeout)) return "";
  }
  if (channel == LINK_OUT && N(r) > 0 && r[0] == '!') {
    secure_server (message_receive (r));
    return "";
  }
  else {
    string back= message_receive (r);
    if (secret != "") back= secret_decode (back, secret);
    success= true;
    return back;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void tm_link_rep::secure_client ( ) [inherited]

Definition at line 92 of file tm_link.cpp.

                            {
  if (secret != "") return;
  write ("!", LINK_IN);
  write_packet (rsa_my_public_key (), LINK_IN);
  bool success;
  string r= read_packet (LINK_OUT, 10000, success);
  if (!success) { stop (); return; }
  secret= rsa_decode (r, rsa_my_private_key ());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void tm_link_rep::secure_server ( string  cmd) [inherited]

Definition at line 83 of file tm_link.cpp.

                                                {
  if (secret != "") return;
  string k= secret_generate ();
  string s= rsa_encode (k, client_public);
  write_packet (s, LINK_IN);
  secret= k;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void tm_link_rep::set_command ( command  _cmd) [inline, inherited]

Definition at line 63 of file tm_link.hpp.

{ feed_cmd = _cmd; }

Implements tm_link_rep.

Definition at line 89 of file socket_link.cpp.

                        {
#ifndef __MINGW32__
  if (alive) return "busy";
  if (DEBUG_AUTO)
    cout << "TeXmacs] Connecting to '" << host << ":" << port << "'\n";
  
  // getting host
  char* _host= as_charp (host);
  struct hostent *hp = gethostbyname (_host);
  tm_delete_array (_host);
  if (hp == NULL) return "Error: no connection for '" * host * "'";

  // creating socket
  io= socket (AF_INET, SOCK_STREAM, 0);
  if (io < 0) return "Error: socket could not be created";

  // connecting to socket
  struct sockaddr_in insock;
  string where= host * ":" * as_string (port);
  memset ((char*) &insock, 0, sizeof (insock));
  insock.sin_family = AF_INET;
  insock.sin_port = htons ((unsigned short) port);
  memcpy ((char*) &insock.sin_addr, hp->h_addr, hp->h_length);
  if (connect (io, (struct sockaddr*) &insock, sizeof (insock)) < 0)
    return "Error: refused connection to '" * where * "'";

  // testing whether it works
  int flags = O_NONBLOCK;
  if (fcntl (io, F_SETFL, flags) < 0)
    return "Error: non working connection to '" * where * "'";
  alive = true;
  sn = socket_notifier (io, &socket_callback, this, NULL);  
  return "ok";
#else
  return "Error: sockets not implemented";
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void socket_link_rep::stop ( ) [virtual]

Implements tm_link_rep.

Definition at line 230 of file socket_link.cpp.

                       {
#ifndef __MINGW32__
  if (!alive) return;
  if (type == SOCKET_SERVER) call ("server-remove", object (io));
  else if (type == SOCKET_CLIENT) call ("client-remove");
  close (io);
  io= -1;
  alive= false;
  sn = socket_notifier ();
  wait (NULL);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

string & socket_link_rep::watch ( int  channel) [virtual]

Implements tm_link_rep.

Definition at line 194 of file socket_link.cpp.

                                   {
  static string empty_string= "";
  if (channel == LINK_OUT) return outbuf;
  else return empty_string;
}
void socket_link_rep::write ( string  s,
int  channel 
) [virtual]

Implements tm_link_rep.

Definition at line 165 of file socket_link.cpp.

                                             {
  if ((!alive) || (channel != LINK_IN)) return;
  if (DEBUG_IO) cout << "---> " << debug_io_string (s) << "\n";
  int len= N(s);
  if (send_all (io, &(s[0]), &len) == -1) {
    cerr << "TeXmacs] write to '" << host << ":" << port << "' failed\n";
    stop ();
  }
}

Here is the call graph for this function:

void tm_link_rep::write_packet ( string  s,
int  channel 
) [inherited]

Definition at line 46 of file tm_link.cpp.

                                                {
  if (secret != "") s= secret_encode (s, secret);
  write ((as_string (N (s)) * "\n") * s, channel);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class tm_link [friend, inherited]

Definition at line 66 of file tm_link.hpp.


Member Data Documentation

bool tm_link_rep::alive [inherited]

Definition at line 40 of file tm_link.hpp.

Definition at line 43 of file tm_link.hpp.

Definition at line 22 of file socket_link.hpp.

Definition at line 25 of file socket_link.hpp.

Definition at line 26 of file socket_link.hpp.

Definition at line 23 of file socket_link.hpp.

int abstract_struct::ref_count [inherited]

Definition at line 142 of file basic.hpp.

Definition at line 41 of file tm_link.hpp.

Definition at line 28 of file socket_link.hpp.

Definition at line 24 of file socket_link.hpp.


The documentation for this struct was generated from the following files: