Esempio n. 1
0
        // This is the call back function, which will be invoked when a client is connected
        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                Socket workerSocket = _Listener.EndAccept(asyn);

                string socketID = Guid.NewGuid().ToString();

                PacketSocket ps = new PacketSocket(workerSocket, socketID);

                _socketList.Add(ps);

                WaitForData(ps, socketID);

                _Listener.BeginAccept(new AsyncCallback(OnClientConnect), null);
            }
            catch (ObjectDisposedException)
            {
                //System.Diagnostics.Debugger.Log(0, "1", "\r\n OnClientConnection: Socket has been closed\r\n");
                _Log.Write(LogType.Error, "\r\nOnDataReceived: Socket has been closed\r\n");
            }
            catch (SocketException se)
            {
                _Log.Write(se);
            }
        }
Esempio n. 2
0
 public bool Add(PacketSocket ps)
 {
     if (_List.ContainsKey(ps.SocketID))
     {
         _List[ps.SocketID] = ps;
     }
     else
     {
         _List.Add(ps.SocketID, ps);
     }
     return(true);
 }
Esempio n. 3
0
        // Start waiting for data from the client
        public void WaitForData(PacketSocket psoc, string socketID)
        {
            try
            {
                if (pfnWorkerCallBack == null)
                {
                    pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                }

                psoc.currentSocket.BeginReceive(psoc.dataBuffer, 0,
                                                psoc.dataBuffer.Length,
                                                SocketFlags.Peek, //Important: this flag enable recive()method get char from begin
                                                pfnWorkerCallBack,
                                                psoc);
            }
            catch (SocketException se)
            {
                _Log.Write(se);
            }
        }
Esempio n. 4
0
        public void OnDataReceived(IAsyncResult asyn)
        {
            PacketSocket psoc = (PacketSocket)asyn.AsyncState;

            try
            {
                if (!psoc.currentSocket.Connected)
                {
                    return;
                }
                IPEndPoint ep = ((IPEndPoint)psoc.currentSocket.RemoteEndPoint);

                int iRx = psoc.currentSocket.EndReceive(asyn);
                if (iRx < 1)
                {
                    return;
                }

                CmdReqBase request = new CmdReqBase();
                string     sLine   = "";
                while (true)
                {
                    int r = ReadLine(ref sLine, psoc.currentSocket);
                    if (r == 0)
                    {
                        return;
                    }
                    if (r <= 0)
                    {
                        throw new Exception("Read data error!");
                    }
                    if (sLine.Trim() == "DONE")
                    {
                        break;
                    }
                    ParseLine(sLine, request);
                }
                _Log.Write(LogType.Debug, "Receive Command Success! Command Data as follow:------", true);
                PackageLog.WritePkgLog((object)request, _Log);
                _Log.Write(LogType.Debug, "---------------Command data end!-------------", true);

                // Treate Command
                CmdRespBase response = new CmdRespBase();
                if (OnClientDataReceived != null)
                {
                    OnClientDataReceived(request, response);
                }
                // Response Command
                sLine = "ErrorCode=" + response.ErrorCode;
                SendLine(sLine, psoc.currentSocket);
                for (int i = 0; i < response.GetParamCount(); i++)
                {
                    sLine = response.GetParamName(i) + "=" + response.GetParamValue(i);
                    SendLine(sLine, psoc.currentSocket);
                }
                sLine = "DONE";
                SendLine(sLine, psoc.currentSocket);

                _Log.Write(LogType.Debug, "Send Command Response Success! Command Data as follow:-----", true);
                PackageLog.WritePkgLog(response, _Log);
                _Log.Write(LogType.Debug, "---------------Command data end!-------------", true);

                // Continue the waiting for data on the Socket
                // WaitForData(psoc, psoc.SocketID); //not need to wait for next request!once connect and disconnecct
                psoc.currentSocket.Close();
                _socketList.Delete(psoc.SocketID);
            }
            catch (ObjectDisposedException)
            {
                //System.Diagnostics.Debugger.Log(0, "1", "\r\nOnDataReceived: Socket has been closed\r\n");
                _Log.Write(LogType.Error, "\r\nOnDataReceived: Socket has been closed\r\n", true);
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    string logmsg = "\r\nClient " + " Disconnected" + "\r\n";
                    _Log.Write(LogType.Error, logmsg, true);

                    // Remove the reference to the worker socket of the closed client
                    // so that this object will get garbage collected
                    psoc.currentSocket.Close();
                    _socketList.Delete(psoc.SocketID);
                }
                else
                {
                    _Log.Write(se);
                }
            }
            catch (Exception exx)
            {
                _Log.Write(exx);
            }
        }