unity3d 异步socket packet序列化

来源:互联网 发布:mac appstore更新不了 编辑:程序博客网 时间:2024/05/20 11:24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using mutils;
 
using scopely.msgpacksharp;
using structs;
 
public class AsyncSocket
{
  
        //单例模式
        private static AsyncSocket instance;
        public Socket clientSocket;
        public string host = "127.0.0.1";
        public int hostPort = 23456;
     
        public static AsyncSocket GetInstance ()
        {
                if (instance == null) {
                        instance = new AsyncSocket ();
                }
                return instance;
        }
 
        public AsyncSocket ()
        {
                //创建Socket对象
                clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //服务器IP地址
                IPAddress ipAddress = IPAddress.Parse (host);
 
                //这是一个异步的建立连接,当连接建立成功时调用connectCallback方法
                IAsyncResult result = clientSocket.BeginConnect (ipAddress, hostPort, new AsyncCallback (connectCallback), clientSocket);
                 
                //当连接超过5秒还没成功表示超时,非必须
                //这段succ变量,偶尔在Mono里报错,实在看不出问题,去掉也没问题
//              bool succ = result.AsyncWaitHandle.WaitOne ();
//              if (!succ) {
//                      //超时
//                      Debug.Log ("time out ");
//                      closeSocket ();
//              }  
        }
 
        private void sendCallback (IAsyncResult asyncConnect)
        {
                int bytesSent = clientSocket.EndSend (asyncConnect);
                Debug.Log ("sendCallback: sent" + bytesSent);
                closeSocket ();
        }
     
        private void connectCallback (IAsyncResult asyncConnect)
        {
                Debug.Log ("EndConnect");
                clientSocket.EndConnect (asyncConnect);
                businessComm ();
        }
 
        private void businessComm ()
        {
                if (!clientSocket.Connected) {
                        Debug.Log ("businessComm clientSocket.Connected false");
                        closeSocket ();
                }
                MemoryStream memStream = new MemoryStream (128);
                memStream.Write (SocketUtils.Int32ToByte (12345), 0, 4);
                memStream.Write (SocketUtils.Int32ToByte (78901), 0, 4);
         
                MyMessage message = new MyMessage ();
                message.MyNumber = 99;
                message.MyString = "abcde";
                byte[] datas = MsgPackSerializer.SerializeObject (message);
         
                memStream.Write (SocketUtils.Int32ToByte (datas.Length), 0, 4);
                memStream.Write (datas, 0, datas.Length);
         
         
                byte[] byteData = memStream.GetBuffer ();
                clientSocket.BeginSend (byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback (sendCallback), clientSocket);
        }
     
        //关闭Socket
        public void closeSocket ()
        {
                Debug.Log ("Socket Closed");
                clientSocket.Close ();
        }
    
}


触发器调用:

?
1
2
3
4
5
6
void OnTriggerEnter (Collider col)
        {
                if (col.gameObject.tag == "Player") {
                        AsyncSocket.GetInstance ();
                }
        }

0 0