Ejemplo n.º 1
0
 private void OnServerCall(object sender, SKServerEventArgs e)
 {
     if (e.type == SKMessage.mestype.TEXT)
     {
         SKServerTextEventArgs et = (SKServerTextEventArgs)e;
         if (textBox1.InvokeRequired)
         {
             Action<SKMessage.textmes> textbox1act = (x) =>
             {
                 textBox1.AppendText("\n");
                 textBox1.AppendText(x.name + " " + x.time.ToString() + "\n");
                 textBox1.AppendText(x.text);
             };
             textBox1.Invoke(textbox1act, et.text_pack);
         }
         c.SendResponse(1, e.ip, DateTime.Now);
     }
     else if (e.type == SKMessage.mestype.EXIT)
     {
         c.SendExit(1, e.ip, DateTime.Now);
     }
 }
Ejemplo n.º 2
0
 private void communication(object RecvServer)
 {
     Socket c = (Socket)RecvServer;
     c.ReceiveTimeout = 10 * 60 * 1000;//10min
     c.ReceiveBufferSize = max_byte_once;
     IPAddress this_ip = ((IPEndPoint)(c.RemoteEndPoint)).Address;
     while (true)
     {
         server_lock.WaitOne();
         if (!started)
         {
             server_communication_sockets.Remove(c);
             if(c != null)
                 c.Close();
             server_lock.ReleaseMutex();
             break;
         }
         server_lock.ReleaseMutex();
         //TODO(_SHADOWK) 并未添加上级主动停止某个ServerSocket工作的代码。
         try
         {
             byte[] head = new byte[head_byte_size];
             int len = c.Receive(head);
             if(len == 0)
                 throw(new Exception());
             if (len == head_byte_size && head[0] == head_2_bytes[0] && head[1] == head_2_bytes[1])
             {
                 ulong len_then = (ulong)BitConverter.ToInt64(head, 2) - head_byte_size - end_byte_size;
                 byte[] then = new byte[len_then];
                 byte[] end = new byte[end_byte_size];
                 ulong len_recv = (ulong)c.Receive(then);
                 int end_recv = c.Receive(end);
                 if (len_recv != len_then || end_recv != end_byte_size || end[0] != end_2_bytes[0] || end[1] != end_2_bytes[1])
                     continue;
                 SKMessage.mestype type;
                 server_lock.WaitOne();
                 if (skmessage.decodemes(then, out type))
                 {
                     switch (type)
                     {
                         case SKMessage.mestype.EXIT:
                             {
                                 SKServerEventArgs exit_event = new SKServerEventArgs();
                                 exit_event.type = SKMessage.mestype.EXIT;
                                 exit_event.ip = ((IPEndPoint)(c.RemoteEndPoint)).Address;
                                 ServerCall(this, exit_event);
                                 server_communication_sockets.Remove(c);
                                 c.Close();
                                 break;
                             }
                         case SKMessage.mestype.RESPONSE:
                             {
                                 SKServerEventArgs response_event = new SKServerEventArgs();
                                 response_event.type = SKMessage.mestype.RESPONSE;
                                 response_event.ip = ((IPEndPoint)(c.RemoteEndPoint)).Address;
                                 ServerCall(this, response_event);
                                 break;
                             }
                         case SKMessage.mestype.TEXT:
                             {
                                 SKServerTextEventArgs text_event = new SKServerTextEventArgs();
                                 text_event.type = SKMessage.mestype.TEXT;
                                 text_event.ip = ((IPEndPoint)(c.RemoteEndPoint)).Address;
                                 text_event.text_pack = skmessage.get_last_text();
                                 ServerCall(this, text_event);
                                 break;
                             }
                         default:
                             {
                                 break;
                             }
                     }
                 }
                 server_lock.ReleaseMutex();
             }
         }
         catch (Exception e)//超时或Socket已关闭
         {
             server_lock.WaitOne();
             SKServerEventArgs exit_event = new SKServerEventArgs();
             exit_event.type = SKMessage.mestype.EXIT;
             exit_event.ip = this_ip;
             ServerCall(this, exit_event);
             server_lock.ReleaseMutex();
             if(c != null)
                 c.Close();
             break;
         }
     }
 }