Beispiel #1
0
        // This is the call back function, which will be invoked when a client is connected
        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                // Here we complete/end the BeginAccept() asynchronous call
                // by calling EndAccept() - which returns the reference to
                // a new Socket object
                Socket workerSocket = _Listener.EndAccept(asyn);

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

                PacketSocket ps = new PacketSocket(workerSocket, socketID);

                _socketList.Add(ps);

                // Let the worker Socket do the further processing for the
                // just connected client
                WaitForData(ps, socketID);

                // Since the main Socket is now free, it can go back and wait for
                // other clients who are attempting to connect
                _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, "OnDataReceived: Socket has been closed\r\n");
            }
            catch (SocketException se)
            {
                //MessageBox.Show(se.Message);
                _Log.Write(se);
            }
        }
Beispiel #2
0
 public bool Add(PacketSocket ps)
 {
     if (_List.ContainsKey(ps.SocketID))
     {
         _List[ps.SocketID] = ps;
     }
     else
     {
         _List.Add(ps.SocketID, ps);
     }
     return(true);
 }
Beispiel #3
0
        // Start waiting for data from the client
        public void WaitForData(PacketSocket psoc, string socketID)
        {
            try
            {
                if (pfnWorkerCallBack == null)
                {
                    pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                }

                //Note: SocketFlags.Peek: 读到缓冲区的数据 仍 可通过Receive取得
                //      SocketFlags.None: 读到缓冲区的数据 不 可通过Receive取得
                psoc.currentSocket.BeginReceive(psoc.dataBuffer, 0,
                                                psoc.dataBuffer.Length,
                                                SocketFlags.Peek,
                                                pfnWorkerCallBack,
                                                psoc);
            }
            catch (SocketException se)
            {
                _Log.Write(se);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Find client Socket by the socketID, and send data to client
        /// </summary>
        /// <param name="package"></param>
        /// <param name="socketID"></param>
        /// <returns></returns>
        private bool SendMsgToClient(byte[] package, string socketID)
        {
            PacketSocket psoc = (PacketSocket)_socketList.Find(socketID);
            string       msg;

            try
            {
                if (psoc == null)
                {
                    throw new Exception("Cannot find PacketSocket whose socketID = " + socketID);
                }


                msg = System.Text.Encoding.GetEncoding(this.CodePageName).GetString(package);

                IPEndPoint ep = ((IPEndPoint)psoc.currentSocket.RemoteEndPoint);

                _Log.Write(LogType.Debug, "--------------- Send Data begin...------------------\r\n", true);
                _Log.Write(LogType.Debug, msg);

                // Send PackageHead
                int iLeave = PackageHeadLength;
                int iSend  = 0;
                while (iLeave > 0)
                {
                    iSend = psoc.currentSocket.Send(package, PackageHeadLength - iLeave, iLeave, SocketFlags.None);
                    if (iSend < 0)
                    {
                        throw new Exception("Error find on the process of sending data!");
                    }
                    iLeave = iLeave - iSend;
                }

                // Send body
                int pLen = Convert.ToInt32(System.Text.Encoding.GetEncoding(this.CodePageName).GetString(package, 0, PackageHeadLength));
                iLeave = pLen;
                while (iLeave > 0)
                {
                    iSend = psoc.currentSocket.Send(package, PackageHeadLength + pLen - iLeave, iLeave, SocketFlags.None);
                    if (iSend < 0)
                    {
                        throw new Exception("Error find on the process of sending data!");
                    }
                    iLeave = iLeave - iSend;
                }
                _Log.Write(LogType.Debug, "\r\nSend Data Success To " + psoc.SocketID + " " + ep.Address.ToString() + ":" + ep.Port.ToString() + "\r\n " + msg + "\r\n");
                return(true);
            }
            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");
                return(false);
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    string logmsg = "Client " + " Disconnected" + "\r\n";
                    _Log.Write(LogType.Error, logmsg);

                    // 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);
                    return(false);
                }
                else
                {
                    _Log.Write(se);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                _Log.Write(ex);
                return(false);
            }
        }
Beispiel #5
0
        public void OnDataReceived(IAsyncResult asyn)
        {
            PacketSocket psoc = (PacketSocket)asyn.AsyncState;

            try
            {
                _Log.Write(LogType.Debug, "-------------- Receive Data Begin..-----------------", true);
                IPEndPoint ep = ((IPEndPoint)psoc.currentSocket.RemoteEndPoint);

                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream by the client
                int iRx = psoc.currentSocket.EndReceive(asyn);
                if (!psoc.currentSocket.Connected)
                {
                    _Log.Write(LogType.Debug, "psoc.currentSocket.Connected=false", true);
                    //return;
                }
                if (iRx < 1)
                {
                    // Close Socket
                    psoc.currentSocket.Close();
                    _socketList.Delete(psoc.SocketID);
                    return;
                }

                byte[] bufHead = new byte[PackageHeadLength];
                int    iLeave  = PackageHeadLength;
                while (iLeave > 0)
                {
                    int iReceive = psoc.currentSocket.Receive(bufHead, PackageHeadLength - iLeave, iLeave, SocketFlags.None);
                    if (iReceive < 0)
                    {
                        throw new Exception("Receive Error! ReceiveCount<0");
                    }
                    iLeave = iLeave - iReceive;
                }

                int pLen = Convert.ToInt32(System.Text.Encoding.GetEncoding(this.CodePageName).GetString(bufHead, 0, PackageHeadLength));

                // Receive Package body
                byte[] bufReceive = new byte[pLen + PackageHeadLength];
                Array.Copy(bufHead, bufReceive, bufHead.Length);
                iLeave = pLen;
                while (iLeave > 0)
                {
                    int iReceive = psoc.currentSocket.Receive(bufReceive, PackageHeadLength + pLen - iLeave, iLeave, SocketFlags.None);
                    if (iReceive < 0)
                    {
                        throw new Exception("Receive Error! ReceiveCount<0");
                    }
                    iLeave = iLeave - iReceive;
                }

                string recMsg = System.Text.Encoding.GetEncoding(this._CodePageName).GetString(bufReceive);
                _Log.Write(LogType.Debug, recMsg);
                _Log.Write(LogType.Debug, "--------------  Receive data success from " + psoc.SocketID + " " + ep.Address.ToString() + ":" + ep.Port.ToString() + "\r\n ");

                recMsg = recMsg.Substring(CommandBase.PackageHeadLength);   //char lenth, not byte lenth
                //Call main program function
                if (OnClientDataReceived == null)
                {
                    _Log.Write(LogType.Warning, "OnClientDataReceived is not implemented!", true);
                    return;
                }

                //byte[] result = OnClientDataReceived(bufReceive);
                string result = OnClientDataReceived(recMsg);

                if (result == null)
                {
                    _Log.Write(LogType.Warning, "No result from OnClientDataReceived !", true);
                    return;
                }


                SendMsgToClient(result, psoc.SocketID);

                // Close Socket
                psoc.currentSocket.Close();
                _socketList.Delete(psoc.SocketID);

                //WaitForData(psoc, 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");
            }
            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);

                    // 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);
                }
            }
        }