Example #1
0
        private void ReceiveCallBack(IAsyncResult result)
        {
            if (!CheckState(result, out string error, out ConnectedObject ConnectedObject))
            {
                Log.PrintMsg(error);
                return;
            }

            try
            {
                if (ConnectedObject.Socket.EndReceive(result) > 0)
                {
                    object receiveData = Map.Deserialize(ConnectedObject.Message);

                    if (receiveData is DataPacket data)
                    {
                        object datapacketProcesed = DataPacketHandler.HandleDataPacket(data);
                        SendReply(ConnectedObject, datapacketProcesed);
                    }
                    else if (receiveData is ClientRequest request)
                    {
                        Log.PrintMsg($"Receive client request: {request}, from {ConnectedObject.Socket.RemoteEndPoint}");
                        object clientrequestProcesed = ClientRequestHanlder.ProccessClientRequest(request);
                        SendReply(ConnectedObject, clientrequestProcesed);
                    }

                    ConnectedObject.Message.Clear();

                    BeginReceive(ConnectedObject);
                }
            }
            catch (SocketException exception)
            {
                Log.PrintMsg(exception);
                CloseClient(ConnectedObject);
                return;
            }
            catch (Exception exception)
            {
                Log.PrintMsg(exception);
                return;
            }
        }