Esempio n. 1
0
 public static void Add(ConnInfo connInfo)
 {
     lock (locker)
     {
         connInfos.Add(connInfo);
     }
 }
Esempio n. 2
0
        public static void Handle(byte[] payloadbytes, ConnInfo connInfo)
        {
            var payloadString = Encoding.UTF8.GetString(payloadbytes);

            if (payloadString.StartsWith("#"))
            {
                connInfo.newsCategory = payloadString.Split("#").LastOrDefault();
                Connections.Add(connInfo);
            }
            else
            {
                PHandler pHandler = JsonConvert.DeserializeObject <PHandler>(payloadString);
                PStorage.Add(pHandler);
            }

            Console.WriteLine(payloadString);
        }
Esempio n. 3
0
        private void AcceptedCallback(IAsyncResult asyncResult)
        {
            ConnInfo conn = new ConnInfo();

            try
            {
                conn.Socket  = _socket.EndAccept(asyncResult);
                conn.Address = conn.Socket.RemoteEndPoint.ToString();
                conn.Socket.BeginReceive(conn.Data, 0, conn.Data.Length,
                                         SocketFlags.None, ReceiveCallback, conn);
            } catch (Exception e)
            {
                Console.WriteLine($"Can't accept: {e.Message}");
            }
            finally
            {
                Accept();
            }
        }
Esempio n. 4
0
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            ConnInfo conn = asyncResult.AsyncState as ConnInfo;

            try
            {
                Socket      senderSocket = conn.Socket;
                SocketError response;
                int         buff_size = senderSocket.EndReceive(asyncResult, out response);
                if (response == SocketError.Success)
                {
                    byte[] payload = new byte[buff_size];
                    Array.Copy(conn.Data, payload, payload.Length);

                    BHandler.Handle(payload, conn);
                }
            } catch (Exception e)
            {
                Console.WriteLine($"Can't receive data:{e.Message}");
            }
            finally
            {
                try
                {
                    conn.Socket.BeginReceive(conn.Data, 0, conn.Data.Length,
                                             SocketFlags.None, ReceiveCallback, conn);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{e.Message}");
                    var address = conn.Socket.RemoteEndPoint.ToString();

                    Connections.Remove(address);
                    conn.Socket.Close();
                }
            }
        }