Beispiel #1
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);
                    }
                }
            }
        }