예제 #1
0
        private void ClientHandler(Socket client)
        {
            string clientIpAddress = client.RemoteEndPoint.ToString();

            if (OnClientConnected != null)
            {
                ClientConnectedEventArgs args = new ClientConnectedEventArgs(clientIpAddress, DateTime.Now);
                OnClientConnected(this, args);
            }

            byte[]       buff = new byte[1024];
            byte[]       tempBuff;
            int          bytesReaded;
            MemoryStream receivedBytesBuffer = new MemoryStream();

            try
            {
                #region 從Client socket 讀取資料流
                while ((bytesReaded = client.Receive(buff, buff.Length, SocketFlags.None)) > 0)
                {
                    tempBuff = new byte[bytesReaded];
                    Array.Copy(buff, tempBuff, bytesReaded);

                    for (int i = 0, n = tempBuff.Length; i < n; i++)
                    {
                        if ((char)tempBuff[i] != Protocol.MessageDelimiter)
                        {
                            receivedBytesBuffer.WriteByte(tempBuff[i]);
                        }
                        else
                        {
                            byte[] dataBytes  = receivedBytesBuffer.ToArray();
                            object recvObject = dataParser.Parse(dataBytes);

                            if (!(recvObject is Bye))
                            {
                                if (recvObject is string)
                                {
                                    if (OnMessageReceived != null)
                                    {
                                        OnMessageReceived(this, recvObject as string, client);
                                    }
                                }
                                else
                                {
                                    if (OnDataReceived != null)
                                    {
                                        OnDataReceived(this, recvObject, client);
                                    }
                                }
                            }
                            else
                            {
                                // 從Data Source Client 端收到準備離線訊息

                                client.Send(Protocol.Bye);
                                client.Shutdown(SocketShutdown.Send);
                            }

                            receivedBytesBuffer.Close();
                            receivedBytesBuffer.Dispose();
                            receivedBytesBuffer = new MemoryStream();
                        }
                    }
                }
                #endregion

                client.Shutdown(SocketShutdown.Receive);
            }
            catch (Exception e)
            {
                #region Client Handler 例外處理
                if (OnException != null)
                {
                    if (e is SocketException)
                    {
                        if (!Protocol.IsIgnorableSocketError((e as SocketException).ErrorCode))
                        {
                            OnException(this, new Exception("Client Handler 發生" + e.GetType().Name + ": " + e.Message + ", Client IP 為 " + clientIpAddress));
                        }
                    }
                    else
                    {
                        OnException(this, new Exception("Client Handler 發生" + e.GetType().Name + ": " + e.Message + ", Client IP 為 " + clientIpAddress));
                    }
                }
                #endregion

                client.Shutdown(SocketShutdown.Both);
            }
            finally
            {
                client.Close();

                lock (synclock)
                {
                    clientsList.Remove(client);
                }
            }

            // 通知ClientDisconnected 事件
            if (OnClientDisconnected != null)
            {
                ClientDisconnectedEventArgs args = new ClientDisconnectedEventArgs(clientIpAddress, DateTime.Now);
                OnClientDisconnected(this, args);
            }
        }
예제 #2
0
        private void ConnectToServer()
        {
            byte[] buff = new byte[128];
            byte[] tempBuff;
            int    bytesReaded;

            MemoryStream    receivedBytesBuffer;
            BinaryFormatter bf = new BinaryFormatter();

            try
            {
                // 連線到InfoServer
                dataServerConnection.Connect(serverHost, serverPort);
                stopped = false;

                // 觸發OnConnected 事件
                if (OnConnected != null)
                {
                    OnConnected(serverHost, serverPort);
                }

                receivedBytesBuffer = new MemoryStream();

                while ((bytesReaded = dataServerConnection.Receive(buff, buff.Length, SocketFlags.None)) > 0)
                {
                    tempBuff = new byte[bytesReaded];
                    Array.Copy(buff, tempBuff, bytesReaded);

                    for (int i = 0, n = tempBuff.Length; i < n; i++)
                    {
                        if ((char)tempBuff[i] != Protocol.MessageDelimiter)
                        {
                            receivedBytesBuffer.WriteByte(tempBuff[i]);
                            continue;
                        }

                        byte[] dataBytes  = receivedBytesBuffer.ToArray();
                        object recvObject = dataParser.Parse(dataBytes);

                        if (!(recvObject is Bye))
                        {
                            if (recvObject is string)
                            {
                                if (OnMessageReceived != null)
                                {
                                    OnMessageReceived(this, recvObject as string, dataServerConnection);
                                }
                            }
                            else
                            {
                                if (OnDataReceived != null)
                                {
                                    OnDataReceived(this, recvObject, dataServerConnection);
                                }
                            }
                        }
                        else
                        {
                            // 收到伺服器送來的結束訊息,則終止傳送資料

                            dataServerConnection.Shutdown(SocketShutdown.Send);
                        }

                        receivedBytesBuffer.Close();
                        receivedBytesBuffer = new MemoryStream();
                    }
                }
            }
            catch (Exception e)
            {
                if (OnException != null)
                {
                    if (e is SocketException)
                    {
                        SocketException se = e as SocketException;

                        if (!Protocol.IsIgnorableSocketError(se.ErrorCode))
                        {
                            OnException(this, e);
                        }
                    }
                    else
                    {
                        OnException(this, e);
                    }
                }
            }
            finally
            {
                if (dataServerConnection != null)
                {
                    dataServerConnection.Close();
                    dataServerConnection = null;

                    if (OnDisconnected != null)
                    {
                        OnDisconnected(serverHost, serverPort);
                    }
                }
            }
        }