Beispiel #1
0
 public void ServerWaitForData(Socket soc, int clientNumber, int clientId, string dateandTime)
 {
     try
     {
         if (pfnWorkerCallBack == null)
         {
             pfnWorkerCallBack = new AsyncCallback(ServerOnDataReceived);// call back function which is to be invoked when there is any write activity by the connected client
         }
         ServerSocketPacket theSocPkt = new ServerSocketPacket(soc, clientNumber, clientId, /*timePoint,*/ dateandTime);
         soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
     }
     catch { }
 }
Beispiel #2
0
        public void ServerOnDataReceived(IAsyncResult asyn)
        {
            ServerSocketPacket socketData = (ServerSocketPacket)asyn.AsyncState;
            string             clientid   = "";

            try
            {
                int           iRx     = socketData.m_currentSocket.EndReceive(asyn);
                int           command = Convert.ToInt32(socketData.dataBuffer[1]);
                string        szData  = "";
                StringBuilder sb      = new StringBuilder();
                for (int j = 0; j < socketData.dataBuffer.Length; j++)
                {
                    sb.Append(Convert.ToString(Convert.ToInt32(socketData.dataBuffer[j])) + " ");
                    //#region logic to cut the zeros from databuffer so log-file is smaller..
                    //if (j < 120)
                    //{
                    //    if (
                    //        (Convert.ToInt64(socketData.dataBuffer[j]) == 0) && (Convert.ToInt64(socketData.dataBuffer[j + 1]) == 0) && (Convert.ToInt64(socketData.dataBuffer[j + 2]) == 0) && (Convert.ToInt32(socketData.dataBuffer[j + 3]) == 0) && (Convert.ToInt32(socketData.dataBuffer[j + 4]) == 0) && (Convert.ToInt32(socketData.dataBuffer[j + 5]) == 0)
                    //      )
                    //    {
                    //        break;
                    //    }
                    //}
                    //#endregion
                }
                szData = sb.ToString();
                if (Convert.ToInt32(socketData.dataBuffer[0]) == 64) // 64=@ do we have a valid header ?
                {
                    #region message recognition section
                    switch (command) // identify command type
                    {
                    case 115:        // Command 's' - which means connect , binary 115
                    {
                        #region connect handling
                        clientid = socketData.dataBuffer[2].ToString() + socketData.dataBuffer[3].ToString() + socketData.dataBuffer[4].ToString() + socketData.dataBuffer[5].ToString() + socketData.dataBuffer[6].ToString();
                        socketData.m_clientId = Convert.ToInt32(clientid);
                        try
                        {
                            if (htSocketList.Contains(socketData.m_clientId))
                            {
                                htSocketList.Remove(socketData.m_clientId);
                            }
                            htSocketList.Add(socketData.m_clientId, socketData);         // save to hash table the clientnumber that defines the socket and the socketdata itself
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Cannot add:" + ex.Message);
                        }
                        break;
                        #endregion
                    }

                    case 66:     // Command 'B' - message broadcast that comes from a client (this is not used yet..)
                    {
                        #region message broadcast
                        clientid = socketData.dataBuffer[2].ToString() + socketData.dataBuffer[3].ToString() + socketData.dataBuffer[4].ToString() + socketData.dataBuffer[5].ToString() + socketData.dataBuffer[6].ToString();
                        string message = System.Text.Encoding.GetEncoding("Windows-1253").GetString(socketData.dataBuffer);
                        message = message.Substring(19, message.IndexOf('^') - 19);
                        // save to db in order to broadcast
                        break;

                        #endregion
                    }
                    }
                    #endregion
                }
                else  // if we detect an invalid command, drop the connection
                {
                    CloseSockets(socketData.m_clientNumber);
                }
                ServerWaitForData(socketData.m_currentSocket, socketData.m_clientNumber, socketData.m_clientId, socketData.m_dateandtime);   // Continue the waiting for data on the Socket
            }
            #region exception handling
            catch (ObjectDisposedException ox)
            {
                //MessageBox.Show(ox.Message);
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054)                                // Error code for Connection reset by peer
                {
                    m_workerSocketList[socketData.m_clientNumber] = null; // Remove the reference to the worker socket of the closed client so that this object will get garbage collected
                }
                else
                {
                }
            }
            #endregion
        }