Back to index

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

#include <socket_server.hpp>

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

List of all members.

Public Member Functions

 socket_server_rep (int port)
 ~socket_server_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 start_client ()
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

int port
int server
array< tm_linkincoming
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_server.hpp.


Constructor & Destructor Documentation

Definition at line 33 of file socket_server.cpp.

                                              :
 port (port2), sn ()
{
  socket_server_set->insert ((pointer) this);
  server= -1;
  alive = false;
}

Definition at line 41 of file socket_server.cpp.

                                       {
  stop ();
  socket_server_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_server_rep::interrupt ( ) [virtual]

Implements tm_link_rep.

Definition at line 154 of file socket_server.cpp.

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

Implements tm_link_rep.

Definition at line 149 of file socket_server.cpp.

                                    {
  (void) msecs;
}

Here is the caller graph for this function:

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

Implements tm_link_rep.

Definition at line 144 of file socket_server.cpp.

                                    {
  (void) channel; 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 75 of file socket_server.cpp.

                          {
#ifndef __MINGW32__
  // get the server
  if ((server = socket (PF_INET, SOCK_STREAM, 0)) == -1)
    return "Error: call to 'socket' failed";

  // lose the pesky "address already in use" error message
  int yes= 1;
  if (setsockopt (server, SOL_SOCKET, SO_REUSEADDR,
                &yes, sizeof (int)) == -1)
    return "Error: call to 'setsockopt' failed";

  // bind
  struct sockaddr_in local_address;
  local_address.sin_family = AF_INET;
  local_address.sin_addr.s_addr = INADDR_ANY;
  local_address.sin_port = htons (6561);
  memset (local_address.sin_zero, '\0', sizeof local_address.sin_zero);
  if (bind (server, (struct sockaddr *) &local_address,
           sizeof (local_address)) == -1)
    return "Error: call to 'bind' failed";

  // listen
  if (::listen (server, 10) == -1)
    return "Error: call to 'listen' failed";

  alive= true;
  
  sn = socket_notifier (server, &socket_server_callback, this, NULL);
  add_notifier (sn);
  
  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:

Definition at line 113 of file socket_server.cpp.

                                 {
#ifndef __MINGW32__
  struct sockaddr_in remote_address;
  socklen_t addrlen= sizeof (remote_address);
  int client= accept (server, (struct sockaddr *) &remote_address, &addrlen);
  if (client == -1) system_warning ("Call to 'accept' failed");
  else {
    string addr= inet_ntoa (remote_address.sin_addr);
    cout << "TeXmacs] opened connection from '" << addr << "'\n";
    array<tm_link> update;
    for (int i=0; i<N(incoming); i++)
      if (incoming[i]->alive)
       update << incoming[i];
    incoming= update;
    incoming << make_socket_link (addr, -1, SOCKET_SERVER, client);
  }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void socket_server_rep::stop ( ) [virtual]

Implements tm_link_rep.

Definition at line 158 of file socket_server.cpp.

                         {
#ifndef __MINGW32__
  // FIXME: close children
  if (!alive) return;
  incoming= array<tm_link> ();
  alive= false;
  
  remove_notifier (sn);
  close (server);
  wait (NULL);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Implements tm_link_rep.

Definition at line 138 of file socket_server.cpp.

                                     {
  static string empty_string= "";
  (void) channel; return empty_string;
}
void socket_server_rep::write ( string  s,
int  channel 
) [virtual]

Implements tm_link_rep.

Definition at line 133 of file socket_server.cpp.

                                               {
  (void) s; (void) channel;
}
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 24 of file socket_server.hpp.

Definition at line 22 of file socket_server.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 23 of file socket_server.hpp.

Definition at line 26 of file socket_server.hpp.


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