Esempio n. 1
0
 private void AcceptCallback(IAsyncResult result)
 {
     ConnectedSocket connected = new ConnectedSocket();
     try
     {
         var socket = (Socket) result.AsyncState;
         connected.Socket = socket.EndAccept(result);
         var key = connected.Socket.Handle.ToInt32();
         if (_clients.ContainsKey(key))
         {
             Logger.Debug("Socket already connected");
             return;
         }
         connected.Buffer = new byte[1024];
         connected.MessageStream = new MemoryStream();
         connected.Stream = new NetworkStream(connected.Socket);
         _clients.TryAdd(key, connected);
         connected.Socket.BeginReceive(connected.Buffer, 0, 5, SocketFlags.None, ReceiveSizeCallback, connected);
     }
     catch (SocketException e)
     {
         HandleSocketException(connected, e);
     }
     catch (Exception e)
     {
         HandleException(connected, e);
     }
     finally
     {
         _serverSocket.BeginAccept(AcceptCallback, result.AsyncState);
     }
 }
Esempio n. 2
0
 public CommandTask(Action<string, ConnectedSocket> onExecutionEnd, string text, ConnectedSocket client)
 {
     _onExecutionEnd = onExecutionEnd;
     _command = text;
     _client = client;
 }
Esempio n. 3
0
 private void TryReadSize(int read, ConnectedSocket connected)
 {
     if (read == 0)
     {
         CloseConnection(connected);
         return;
     }
     var codedInputStream = CodedInputStream.CreateInstance(connected.Buffer, 0, 5);
     var length = codedInputStream.ReadRawVarint32();
     var intSize = CodedOutputStream.ComputeRawVarint32Size(length);
     connected.ReadLeft = (int) (length + intSize - 5);
     connected.MessageStream.Write(connected.Buffer, intSize, 5 - intSize);
     connected.Socket.BeginReceive(
         connected.Buffer,
         0,
         Math.Min(connected.ReadLeft, connected.Buffer.Length),
         SocketFlags.None,
         ReceiveMessageCallback,
         connected);
 }
Esempio n. 4
0
 private void ParseAndDoWork(ConnectedSocket connected)
 {
     var message =
         Message.ParseFrom(CodedInputStream.CreateInstance(connected.MessageStream.GetBuffer(), 0,
             (int) connected.MessageStream.Length));
     connected.MessageStream.SetLength(0);
     switch (message.Type)
     {
         case Message.Types.Type.MESSAGE:
             ThreadPool.QueueUserWorkItem(BroadcastMessage,
                 new KeyValuePair<Message, ConnectedSocket>(message, connected));
             break;
         case Message.Types.Type.COMMAND:
             CommandWorker.PutTask(new CommandTask((s, connectedSocket) =>
             {
                 var msg = Message.CreateBuilder()
                     .SetAuthor("SERVER")
                     .SetType(Message.Types.Type.MESSAGE)
                     .SetText(s).Build();
                 msg.WriteDelimitedTo(connectedSocket.Stream);
             },
                 message.Text,
                 connected));
             break;
     }
     connected.Socket.BeginReceive(connected.Buffer, 0, 5, SocketFlags.None, ReceiveSizeCallback,
         connected);
 }
Esempio n. 5
0
 private void HandleSocketException(ConnectedSocket connected, SocketException e)
 {
     CloseConnection(connected);
     Logger.Error("Socket error: " + e.Message);
 }
Esempio n. 6
0
 private void CloseConnection(ConnectedSocket connected)
 {
     connected.Socket.Close();
     _clients.TryRemove(connected.Socket.Handle.ToInt32(), out connected);
 }